package com.snap.update.utils;

import android.util.Base64;
import android.util.Log;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
 * 需要加密数据采用AES-128加密，加密模式为CBC，填充方式为pkcs7padding，秘钥为kyoPcMQ&UP1q4PVr ，偏移量为 0eRuV07yZlMAEsAO
 * 其中AES-128和AES-256的区别，只要把密钥长度由16改成32就好
 */
public class AESUtil {
    /**
     * 算法/模式/填充
     */
    private static final String CipherMode = "AES/CBC/PKCS5Padding";
    /**
     * 密钥
     */
    private static final String password = "kyoPcMQ&UP1q4PVr";
    /**
     * 偏移量
     */
    private static final String iv = "0eRuV07yZlMAEsAO";


    //--加密
    public static byte[] encrypt(byte[] content) {
        byte[] result = encryptByte2Byte(content, password, iv);
        return result;
    }

    //--解密
    public static byte[] decrypt(byte[] content) {
        byte[] result = decryptByte2Byte(content, password, iv);
        return result;
    }

    //--创建密钥
    private static SecretKeySpec createKey(String password) {
        byte[] data = null;
        if (password == null) {
            password = "";
        }
        StringBuffer sb = new StringBuffer();
        sb.append(password);
        String s = null;
        while (sb.length() < 16) {
            sb.append(" ");//--密码长度不够16补足到16
        }
        s = sb.substring(0, 16);//--截取16位密码
        try {
            data = s.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new SecretKeySpec(data, "AES");
    }

    //--创建偏移量
    private static IvParameterSpec createIV(String iv) {
        byte[] data = null;
        if (iv == null) {
            iv = "";
        }
        StringBuffer sb = new StringBuffer(16);
        sb.append(iv);
        String s = null;
        while (sb.length() < 16) {
            sb.append(" ");//--偏移量长度不够16补足到16
        }
        s = sb.substring(0, 16);//--截取16位偏移量
        try {
            data = s.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return new IvParameterSpec(data);
    }

    //--加密字节数组到字节数组
    private static byte[] encryptByte2Byte(byte[] content, String password, String iv) {
        try {
            SecretKeySpec key = createKey(password);
            Cipher cipher = Cipher.getInstance(CipherMode);
            cipher.init(Cipher.ENCRYPT_MODE, key, createIV(iv));
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //--加密字符串到base64
    private static String encryptString2Base64(String content, String password, String iv) {
        byte[] data = null;
        try {
            data = content.getBytes("UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
        data = encryptByte2Byte(data, password, iv);
        String result = new String(Base64.encode(data, Base64.DEFAULT));
        return result;
    }

    //-- 解密字节数组到字节数组
    private static byte[] decryptByte2Byte(byte[] content, String password, String iv) {
        try {
            SecretKeySpec key = createKey(password);
            Cipher cipher = Cipher.getInstance(CipherMode);
            cipher.init(Cipher.DECRYPT_MODE, key, createIV(iv));
            byte[] result = cipher.doFinal(content);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //-- 解密字节数组到字符串
    private static String decryptByte2String(byte[] content, String password, String iv) {
        byte[] data = decryptByte2Byte(content, password, iv);
        String result = new String(data);
        return result;
    }

    //-- 解密字节数组到字符串
    private static String decryptBase642String(String content, String password, String iv) {
        byte[] data = Base64.decode(content, Base64.DEFAULT);
        String result = decryptByte2String(data, password, iv);
        return result;
    }

    private void use() {
        String base64String = AESUtil.encryptString2Base64("欢迎来到王者荣耀", "kakuishdyshifncgyrsjdiosfnvjfeas", "asadfdedwderfvgd");
        Log.i("load_Aes", " base64String " + base64String);//--U0E01UZ5ed7hijY7YY1+v/x5rsbWtknI9piWP3QHvfQ=

        String string = AESUtil.decryptBase642String(base64String, "kakuishdyshifncgyrsjdiosfnvjfeas", "asadfdedwderfvgd");
        Log.i("load_Aes", " string " + string);//--欢迎来到王者荣耀
    }
}
